package conversation

import (
	"coze_for_sdk/controller"
	"coze_for_sdk/model"

	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator"
)

func Update(c *gin.Context) {
	//入参接收
	updateParams := struct {
		ConversationId string `json:"conversation_id" validate:"required"`
		Title          string `json:"title" validate:"required"`
	}{}

	if err := c.ShouldBindJSON(&updateParams); err != nil {
		controller.Fail(c, controller.ParamsError, err.Error())
		return
	}

	//参数验证
	validate := validator.New()
	if err := validate.Struct(updateParams); err != nil {
		controller.Fail(c, controller.ParamsError, err.Error())
		return
	}

	userId := c.GetInt("user_id")
	db := model.GetInstance().GetDB()

	//会话和自己不匹配
	var conversation model.CozeConversation
	conversation, err := conversation.GetConversation(updateParams.ConversationId, userId)
	if err != nil {
		controller.Fail(c, controller.BusinessError, "会话不存在")
		return
	}

	db.Model(&model.CozeConversation{}).
		Where("conversation_id = ?", updateParams.ConversationId).
		Updates(map[string]interface{}{
			"title": updateParams.Title,
		})

	controller.Success(c, nil)

}
